#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>

#define High 600
#define Width 350

#pragma comment(lib,"Winmm.lib")

IMAGE img_bk, img_bd1, img_bd2, img_bar_up1, img_bar_up2, img_bar_down1, img_bar_down2;
int bird_x;
int bird_y;

int stone_x1;
int stone_y1;

int stone_x2;
int stone_y2;

int death;

int score;
TCHAR s[100];

void startup()
{
	mciSendString(TEXT("open D:\\background.mp3 alias bkmusic"), NULL, 0, NULL);//背景音乐
	mciSendString(TEXT("play bkmusic repeat"), NULL, 0, NULL);				//循环播放


	initgraph(Width, High);
	loadimage(&img_bk, _T("D:\\background.jpg"));
	loadimage(&img_bd1, _T("D:\\bird1.jpg"));	
	loadimage(&img_bd2, _T("D:\\bird2.jpg"));
	loadimage(&img_bar_up1, _T("D:\\bar_up1.gif"));
	loadimage(&img_bar_up2, _T("D:\\bar_up2.gif"));
	loadimage(&img_bar_down1, _T("D:\\bar_down1.gif"));
	loadimage(&img_bar_down2, _T("D:\\bar_down2.gif"));

	bird_x = 50;
	bird_y = 250;

	stone_x1 = Width / 2;
	stone_y1 = -High / 2;
	stone_x2 = 350+Width / 2;
	stone_y2 = -High / 3;

	score = 0;

	death = 0;

	BeginBatchDraw();
}

void show()
{
	putimage(0, 0, &img_bk);
	putimage(stone_x1, stone_y1, &img_bar_up1, NOTSRCERASE);
	putimage(stone_x1, stone_y1, &img_bar_up2, SRCINVERT);		//显示上面的障碍物
	putimage(stone_x1, stone_y1+700, &img_bar_down1, NOTSRCERASE);
	putimage(stone_x1, stone_y1+700, &img_bar_down2, SRCINVERT);		//显示下面的障碍物
	putimage(stone_x2, stone_y2, &img_bar_up1, NOTSRCERASE);
	putimage(stone_x2, stone_y2, &img_bar_up2, SRCINVERT);		//显示上面的障碍物
	putimage(stone_x2, stone_y2 + 700, &img_bar_down1, NOTSRCERASE);
	putimage(stone_x2, stone_y2 + 700, &img_bar_down2, SRCINVERT);		//显示下面的障碍物
	putimage(bird_x, bird_y, &img_bd1, NOTSRCERASE);
	putimage(bird_x, bird_y, &img_bd2, SRCINVERT);		//显示小鸟
	_stprintf(s, _T("%d"), score);
	settextstyle(30,30,0);
	outtextxy(Width / 6, High / 6, s);
	if (death == 1)
	{
		outtextxy(0, High / 2, _T("GAME OVER!"));
	}

	FlushBatchDraw();

	if (death == 1)
	{
		system("pause");
		exit(0);
	}

	Sleep(50);
}

void updateWithoutInput()
{
	if (bird_y < 580)
		bird_y += 3;				 //小鸟下降

	if (stone_x1 > -140)
		stone_x1--;
	else if (stone_x1 == -140)
	{
		srand(time(0));
		stone_x1 = 350 + Width / 2;
		stone_y1 = -100-rand() % 500;
	}
	if (stone_x2 > -140)
		stone_x2--;
	else if (stone_x2 == -140)
	{
		stone_x2 = 350 + Width / 2;
		stone_y2 = -100-rand() % 500;
	}								//生成障碍物

	if (bird_x == stone_x1+140 || bird_x == stone_x2+140)
		score++;
	if (bird_x+34 >= stone_x1 && bird_x <= stone_x1 + 140 && bird_y >= 0 && bird_y <= stone_y1 + 600)
	{
		death = 1;
	}
	if (bird_x+34 >= stone_x1 && bird_x <= stone_x1 + 140 && bird_y+24 >= stone_y1 + 700 && bird_y+24 <= 600)
	{
		death = 1;
	}

	if (bird_x+34 >= stone_x2 && bird_x <= stone_x2 + 140 && bird_y >= 0 && bird_y <= stone_y2 + 600)
	{
		death = 1;
	}
	if (bird_x+34 >= stone_x2 && bird_x <= stone_x2 + 140 && bird_y+24 >= stone_y2 + 700 && bird_y+24 <= 600)
	{
		death = 1;
	}

	if(bird_y+24==600)
	{
		death = 1;
	}
}

void updateWithInput()
{
	char input;
	if (kbhit())
	{
		input = getch();
		if (input == ' ' && bird_y > 20)
		{
			bird_y -= 60;
			mciSendString(TEXT("close jpmusic"), NULL, 0, NULL);	//把前面一次的音乐关上
			mciSendString(TEXT("open D:\\Jump.mp3 alias jpmusic"), NULL, 0, NULL);//打开音乐
			mciSendString(TEXT("play jpmusic"), NULL, 0, NULL);		//播放一次
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}
int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示新画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}